package com.baichang.taoshunda.dao.impl;

import com.baichang.taoshunda.dao.GoodsSpecValueDao;
import com.baichang.taoshunda.entity.GoodsSpecValue;
import com.baichang.framework.dao.impl.BaseDaoImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Map;

@Repository
public class GoodsSpecValueDaoImpl extends BaseDaoImpl<GoodsSpecValue> implements GoodsSpecValueDao {

    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Override
    public List<Map<String, Object>> findGoodsSpecValues(Integer specId) {
        String sql = " SELECT * FROM {table} WHERE spec_id=? ";
        return queryForMapList(sql, specId);
    }

    @Override
    public boolean updateSpec(Integer specValueId, String valueName, Double price) {
        return false;
    }

    /**
     * 删除商品规格
     *
     * @param isPrice
     * @param goodsId
     * @return
     */
    @Override
    public boolean deleteBySpecId(Integer isPrice, Integer goodsId) {
        String sql = "DELETE  FROM t_goods_spec_value where spec_id in (SELECT id FROM t_goods_spec  WHERE goods_id=? AND is_price=?) ";
        return update(sql, goodsId, isPrice) > 0;
    }

    @Override
    public boolean deleteBySpecValueId(Integer specId) {
        String sql = "DELETE FROM t_goods_spec_value  WHERE spec_id=?";
        return update(sql, specId) > 0;
    }

    /**
     * 查询对应商品的规格的价格
     *
     * @param goodsId    商品编号
     * @param specValues 商品规格
     * @return
     */
    @Override
    public double getPriceBySpecAndGoodsId(int goodsId, String specValues) {
        String sql = "SELECT IF(0=count(price),0,price) FROM t_goods_spec_value spv INNER JOIN t_goods_spec sp ON sp.id=spv.spec_id WHERE sp.goods_id=? AND value_name=?";
        return jdbcTemplate.queryForObject(sql, Double.class, goodsId, specValues);
    }
}